package com.jianyun.jyzs.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.Hashtable;
import java.util.Map;

/**
 * 类描述：FileUtil
 */
public class FileUtil {
	class FileType {
		public static final int AUDIO=1;
		public static final int VIDEO=2;
		public static final int IMAGE=3;
		public static final int TEX=4;
		public static final int OTHER=5;
		public static final int WORD=6;
		public static final int EXCEL=7;
		public static final int PDF=8;
		public static final int RAR=9;
		public static final int PPT=11;
		public static final int FOLDER=10;
		public static final int DWG=12;



		public static final int FILE=20;

		public static final int All=100;


		public static final int UPTO=30;

		public static final int selected=10086;
		public static final int unselect=10010;
		public static final int forbid=10000;
	}
	
	public static Map<String,String> map;
	
	static{
		if(map == null){
			 map = new Hashtable<String, String>();
			 map.put(".3gp",    "video/3gpp");
	         map.put(".apk",    "application/vnd.android.package-archive");
	         map.put(".asf",    "video/x-ms-asf");
	         map.put(".avi",    "video/x-msvideo");
	         map.put(".bin",    "application/octet-stream");
	         map.put(".bmp",    "image/bmp");
	         map.put(".c",  "text/plain");
	         map.put(".class",  "application/octet-stream");
	         map.put(".conf",   "text/plain");
	         map.put(".cpp",    "text/plain");
	         map.put(".doc",    "application/msword");
	         map.put(".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
	         map.put(".xls",    "application/vnd.ms-excel"); 
	         map.put(".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	         map.put(".exe",    "application/octet-stream");
	         map.put(".gif",    "image/gif");
	         map.put(".gtar",   "application/x-gtar");
	         map.put(".gz", "application/x-gzip");
	         map.put(".h",  "text/plain");
	         map.put(".htm",    "text/html");
	         map.put(".html",   "text/html");
	         map.put(".jar",    "application/java-archive");
	         map.put(".java",   "text/plain");
	         map.put(".jpeg",   "image/jpeg");
	         map.put(".jpg",    "image/jpeg");
	         map.put(".js", "application/x-javascript");
	         map.put(".log",    "text/plain");
	         map.put(".m3u",    "audio/x-mpegurl");
	         map.put(".m4a",    "audio/mp4a-latm");
	         map.put(".m4b",    "audio/mp4a-latm");
	         map.put(".m4p",    "audio/mp4a-latm");
	         map.put(".m4u",    "video/vnd.mpegurl");
	         map.put(".m4v",    "video/x-m4v"); 
	         map.put(".mov",    "video/quicktime");
	         map.put(".mp2",    "audio/x-mpeg");
	         map.put(".mp3",    "audio/x-mpeg");
	         map.put(".amr",    "audio/x-mpeg");
	         map.put(".mp4",    "video/mp4");
	         map.put(".mpc",    "application/vnd.mpohun.certificate");       
	         map.put(".mpe",    "video/mpeg");  
	         map.put(".mpeg",   "video/mpeg");  
	         map.put(".mpg",    "video/mpeg");  
	         map.put(".mpg4",   "video/mp4");   
	         map.put(".mpga",   "audio/mpeg");
	         map.put(".msg",    "application/vnd.ms-outlook");
	         map.put(".ogg",    "audio/ogg");
	         map.put(".pdf",    "application/pdf");
	         map.put(".png",    "image/png");
	         map.put(".pps",    "application/vnd.ms-powerpoint");
	         map.put(".ppt",    "application/vnd.ms-powerpoint");
	         map.put(".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation");
	         map.put(".prop",   "text/plain");
	         map.put(".rc", "text/plain");
	         map.put(".rmvb",   "audio/x-pn-realaudio");
	         map.put(".rtf",    "application/rtf");
	         map.put(".sh", "text/plain");
	         map.put(".tar",    "application/x-tar");   
	         map.put(".tgz",    "application/x-compressed"); 
	         map.put(".txt",    "text/plain");
	         map.put(".wav",    "audio/x-wav");
	         map.put(".wma",    "audio/x-ms-wma");
	         map.put(".wmv",    "audio/x-ms-wmv");
	         map.put(".wps",    "application/vnd.ms-works");
	         map.put(".xml",    "text/plain");
	         map.put(".z",  "application/x-compress");
	         map.put(".zip",    "application/x-zip-compressed");
		}
	}

	public static String getDataSize(long paramLong){
		DecimalFormat localDecimalFormat = new DecimalFormat("###.00");
		if (paramLong < 1024L){
			return paramLong + "bytes";
		}else if (paramLong < 1048576L){
			return localDecimalFormat.format((float)paramLong / 1024.0F) + "KB";
		}else if (paramLong < 1073741824L){
			return localDecimalFormat.format((float)paramLong / 1024.0F / 1024.0F) + "MB";
		}else if (paramLong < 0L){
			return localDecimalFormat.format((float)paramLong / 1024.0F / 1024.0F / 1024.0F) + "GB";
		}
		return "error";
	}


    //获取文件的种类
    public static int getFileType(String name) {
        int type = FileType.OTHER;

        String last = getFileExtName(name);
        if (last == null){
            return type;
        }
        if (last.equals("jpg") || last.equals("png") || last.equals("bmp") || last.equals("gif")
        		|| last.equals("jpeg") || last.equals("asf")) {
            type = FileType.IMAGE;
        } else if (last.equals("avi") || last.equals("mp4") || last.equals("mkv") || last.equals("flv") 
        		|| last.equals("3gp") || last.equals("asf") || last.equals("m4u") || last.equals("m4v") 
        		|| last.equals("mov") || last.equals("mpe") || last.equals("mpeg") || last.equals("mpg") 
        		|| last.equals("mpg4")) {
            type = FileType.VIDEO;
        } else if (last.equals("txt") || last.equals("c") || last.equals("h") || last.equals("htm")
	       		|| last.equals("html") || last.equals("java") || last.equals("log") || last.equals("prop")
	    		|| last.equals("rc") || last.equals("sh") || last.equals("xml")) {
            type = FileType.TEX;
        } else if (last.equals("mp3") || last.equals("m3u") || last.equals("m4a") || last.equals("m4b")
	    		|| last.equals("m4p") || last.equals("mp2") || last.equals("amr") || last.equals("mpga")
	    		|| last.equals("ogg") || last.equals("rmvb") || last.equals("wav") || last.equals("wma")
	    		|| last.equals("wmv")) {
            type = FileType.AUDIO;
        } else if (last.equals("doc") || last.equals("docx")) {
        	type = FileType.WORD;
        } else if (last.equals("xls") || last.equals("xlsx")) {
        	type = FileType.EXCEL;
        } else if (last.equals("pdf")) {
        	type = FileType.PDF;
        } else if (last.equals("rar") || last.equals("zip") || last.equals("z")) {
        	type = FileType.RAR;
        } else if (last.equals("ppt") || last.equals("pptx")) {
        	type = FileType.PPT;
        } else if (last.equals("dwg")) {
        	type = FileType.DWG;
        }
        return type;
    }
    
	/**
	 * 获取文件扩展名
	 * @param filePath
	 * @return
	 */
	public static String getFileExtName(String filePath){
		String fileExtName = "";
		try{
			fileExtName = filePath.substring(filePath.lastIndexOf(".")+1);
		}catch(Exception e){}
		return fileExtName;
	}
	
	/**
	 * 获取文件的文件夹名
	 * @param filePath
	 * @return
	 */
	public static String getFileFolder(String filePath){
		String fileFolder = "";
		try{
			fileFolder = filePath.substring(0,filePath.lastIndexOf("/"));
		}catch(Exception e){}
		return fileFolder;
	}
	/**
	 * 获取包含后缀的文件名
	 * @param filePath
	 * @return
	 */
	public static String getFileName(String filePath){
		try{
			File file =new File(filePath);
			return file.getName();
		}catch(Exception e){}
		
		return "";
	}
	/**
	 * 处理文件名超过10个字符的串
	 * @param fileNoExtName
	 * @return
	 */
	public static String processFileName(String fileNoExtName){
		try{
			if(fileNoExtName != null && fileNoExtName.length() > 10){
				fileNoExtName = fileNoExtName.substring(0,7)+"..."+fileNoExtName.substring(fileNoExtName.length()-3);
			}
		}catch(Exception e){}
		return fileNoExtName;
	}
	/**
	 * 获取不包含后缀的文件名
	 * @param filePath
	 * @return
	 */
	public static String getFileNoExtName(String filePath){
		try{
			File file =new File(filePath);
			return file.getName().split("\\.")[0];
		}catch(Exception e){}
		
		return "";
	}
	/**
	 * 后缀类型
	 * @return
	 */
	public static String getAppType(String key){
		if(key != null && !key.trim().equals("")){
			if(map.get(key) == null){
				return "application/*";
			}
			return map.get(key);
		}else{
			return "application/*";
		}
		 
	}
	//文件拷贝
    //要复制的目录下的所有非子目录(文件夹)文件拷贝
    public static boolean CopySdcardFile(String fromFile, String toFile){
        try{
            InputStream fosfrom = new FileInputStream(fromFile);
            OutputStream fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = fosfrom.read(bt)) > 0)
            {
                fosto.write(bt, 0, c);
            }
            fosfrom.close();
            fosto.close();
            return true;
        }catch (Exception ex){
            return false;
        }
    }
}